import requests
from flask import request, render_template

from app.web.casesuit.test_register_code import  RegisterCode
from app.web.casesuit.test_get_token import  GetToken
from app.web.comm.random_number import  Randoms
from app.web.comm.backend_login import  LoginBackend
import time
from app.web.comm.check import Check
# from casesuit.shanghu.config_shanghu  import  *
from app.web.conf.config import  *
from . import web
C=Check()
LB=LoginBackend()
# base_url_old='https://adminyp.yewifi.com'
#
# base_url="https://shanghu-backend-test.wetax.com.cn"
username = "1811108@gaopeng.com"#服务商

#pc注册不绑定微信
@web.route("/shanghu/register", methods=['GET', 'POST'])
# def Merchant_pc_register_moni():
def shanghu_register():
    if request.method == "POST":
        channel=request.values.get("shanghu_channel")

        # provider_id = C.coonect_db(database='fpmerchant', sql="select id from m_service_provider where username='%s'" % username)[0]
        provider_id=605
        headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}
        #取GetToken_pc返回的token
        token = GetToken().token_pc_register_random()
        url=base_url+'/v2/client/create-info?token=%s'%token
        print(url)
        data={
                "name": "接口注册测试-百望云",#企业名称
                "client_nickname": "企业昵称店",#企业昵称
                "tax_code": "tax_code",#企业税号
                # "sell_address": "浙江省嘉兴市海宁市海洲街道海昌南路509号16层",#企业地址
                "sell_address":"浙江省嘉兴市海宁市海洲街道海昌南路509号16层",
                "sell_phone": "0713-7222222",#企业电话
                "sell_bank_name": "招商银行",#企业开户银行
                "sell_bank_account": "123456788",#开户银行账号
                "sell_licence_image": "",#营业执照
                "legal_person_name": "杨二位",#法人姓名
                "drawer": "开票员",#开票人姓名
                "industry": "",#所属行业
                "email": "jenny.zhang@wetax.com.cn",#申请人邮箱
                "registration_code": "registration_code",#注册码
                "applicant_name": "申请de",#申请人姓名
                "applicant_phone": "applicant_phone",#申请人手机号
                "message_code": "短信验证码",#短信验证码
                "password": "zxz123456",#平台登陆密码
                "repeat": "zxz123456",#确认登陆密码
                "agree":"true",#勾选阅读项
                # "provinceArr": [
                #     {"code": "120000","name": "天津市"},
                #     {"code": "120100","name": "市辖区"},{"code": "120101","name": "和平区"},{"code": "120101001", "name": "劝业场街道"}],
                "provinceArr":[{"code":"510000","name":"四川省"},{"code":"510300","name":"自贡市"},{"code":"510302","name":"自流井区"},{"code":"510302003","name":"新街街道"}],
                "province":"四川省","city":"自贡市","region_code":"510302003"

                # "province": "天津市",
                # "city": "和平区",
                # "region_code": "120101"
        }


        #取公共层获取的随机税号、手机号已经生成的注册码
        data['tax_code']=Randoms().tax_code()

        # data['tax_code']=Randoms().tax_code_c()#获取重庆地区税号

        # data['tax_code']="912500107MA5RH09M0R0"#重庆地区税号注册拦截
        # data['applicant_phone']='15233444055'
        data['applicant_phone']=Randoms().telephone()
        # data["applicant_phone"]="17665280411"
        # data['applicant_phone']="53592899"
        data['registration_code']=RegisterCode().test_register_code(channel,provider_id)#根据渠道生成注册码
        # data['registration_code'] =''#注册码为空
        # data['sell_address']='大冲商务中心'#注册地址

        print("注册信息：%s"%data)
        response = requests.post(headers=headers, url=url, json=data, verify=False)
        a=response.text
        print("注册返回：%s"%a)
        #self.assertEqual(a['code'], 0)
        # time.sleep(10)

        try:
            register_status= C.coonect_db(database='fpmerchant', sql="select register_status from m_sell_client_new where tax_code='%s'" % data['tax_code'])[0][0]
            print(type(register_status))
            if register_status == 1:
                print('已经审核')

            elif register_status == 0:
                LB.test_cerficate_merchant(data['tax_code'], username)
                # print("未审核")

            else:
                print("商户注册异常")

            # return ('商户平台税号：%s 注册成功' %data['tax_code'])
            return render_template("register.html",cont2="商户平台注册税号："+data['tax_code'])

        except Exception as e:
            print(e)
            return ("数据库没有找到该商户！！")

    # return render_template('register.html')




# pc注册绑定微信
#     def Merchant_pc_register_bang(self):
#         provider_id = C.coonect_db(database='fpmerchant',
#                                    sql="select id from m_service_provider where username='%s'" % self.username)[0]
#         headers = {"Accept": "application/json, text/plain, */*", "Content-Type": "application/json;charset=UTF-8"}
#         # 取GetToken_pc返回的token
#         token = GetToken().token_pc_register_band()
#         url = base_url  + '/v2/client/create-info?token=%s' % token
#         data = {
#             "name": "zxz绑定微信",  # 企业名称
#             "client_nickname": "fx参天眼药水企业昵称店",  # 企业昵称
#             "tax_code": "tax_code",  # 企业税号
#             "sell_address": "高灯科技产品体验区27楼",  # 企业地址
#             "sell_phone": "0713-7222222",  # 企业电话
#             "sell_bank_name": "招商银行",  # 企业开户银行
#             "sell_bank_account": "123456788",  # 开户银行账号
#             "sell_licence_image": "",  # 营业执照
#             "legal_person_name": "法人",  # 法人姓名
#             "drawer": "开票员",  # 开票人姓名
#             "industry": "",  # 所属行业
#             "email": "jenny.zhang@wetax.com.cn",  # 申请人邮箱
#             "registration_code": "",  # 注册码
#             "applicant_name": "申请人姓名",  # 申请人姓名
#             "applicant_phone": "applicant_phone",  # 申请人手机号
#             "message_code": "短信验证码",  # 短信验证码
#             "password": "zxz123456",  # 平台登陆密码
#             "repeat": "zxz123456",  # 确认登陆密码
#             "agree": "true" , # 勾选阅读项
#            "provinceArr":[{"code": "150000", "name": "内蒙古自治区"}, {"code": "150600", "name": "鄂尔多斯市"},
#                           {"code": "150623", "name": "鄂托克前旗"},
#                           {"code": "150623400", "name": "上海庙经济开发区"}],
#             "province": "内蒙古自治区",
#             "city": "鄂尔多斯市",
#             "region_code": "150623400"
#
#         }
#         print('''''''''''''')
#         print(data)
#         #
#         # {"name": "PC老版注册", "client_nickname": "", "tax_code": "GAPEN2019082001", "sell_address": "大冲商务中心",
#         #  "sell_phone": "167788899993", "sell_bank_name": "中国银行（深圳支行）", "sell_bank_account": "6756546477474747",
#         #  "registration_code": "", "drawer": "张小增",
#         #  "provinceArr": [{"code": "150000", "name": "内蒙古自治区"}, {"code": "150100", "name": "呼和浩特市"},
#         #                  {"code": "150101", "name": "市辖区"}], "applicant_name": "申请人", "applicant_phone": "15677889998",
#         #  "message_code": "1111", "email": "", "password": "zxz123456", "repeat": "zxz123456", "industry": "",
#         #  "legal_person_name": "法人", "sell_licence_image": "", "province": "内蒙古自治区", "city": "呼和浩特市",
#         #  "region_code": "150101"}
#
#
#
#         # 取公共层获取的随机税号、手机号已经生成的注册码
#         data['tax_code'] = Randoms().tax_code()
#         # data['tax_code'] = '110101201801010113'
#         data['applicant_phone'] = Randoms().telephone()
#         # data['applicant_phone']="53592899"
#         data['registration_code'] = RegisterCode().test_register_code(channel, provider_id)#获取注册码
#         print("注册信息：%s" % data)
#         response = requests.post(headers=headers, url=url, json=data, verify=False)
#         a = response.text
#         # print("注册请求：%s" % data)
#         print("注册返回：%s" % a)
#         # self.assertEqual(a['code'], 0)
#         # time.sleep(10)
#         register_status = C.coonect_db(database='fpmerchant',
#                                        sql="select register_status from m_sell_client_new where tax_code='%s'" %
#                                            data['tax_code'])[0][0]
#         print(type(register_status))
#         if register_status == 1:
#             print('已经审核')
#
#         elif register_status == 0:
#             LB.test_cerficate_merchant(data['tax_code'], self.username)
#             # print("未审核")
#
#         else:
#             print("商户注册异常")
#
#         print('商户平台注册接口：%s' % a)



if __name__ == '__main__':
    username = "1811108@gaopeng.com"#接口服务商新a04
    # username="187@www.com"#201811051490492服务商黑名单

    # username ="18080403@gaopeng.com"#2018080491717726  黑名单
    # username = "18102901@gaopeng.com"# 安徽来购商业连锁 2018102902403737 接口服务商新a03
    # username = "18080403@gaopeng.com"  # 海鼎地产云票儿插件  2018080491717726  海鼎
    # username = "tengyun"  # 云南腾云信息产业有限公司 2018092723100427  云南腾云信息产业有限公司
    # username = "bjetcp"  #北京悦畅科技有限公司  2018083034113363  (ETCP)北京悦畅科技有限公司
    # channel="GP110003"#GP110003北京百望云
    # channel = "GP110002"  # GP110002北京百望
    # channel="GP110000"#GP110000大象慧云
    # channel="GP110001"#GP110001北京票通
    channel = "GP518001"# GP518001自研专业版
    # channel = "GP519000"# GP519000高灯数字发票
    # channel ="GP530001"#成都点下户
    # channel =GP116000#万达儿童业态
    # channel ="GP518004"#成都专业版"gp_20191017192215_605_12446_37159"
    # channel = "GP519001"#
    # channel = "GP610001"#WCS
    # provider_id = "953"  # 服务商id--接口服务商新a04 605  878  953(香格里拉酒店)
    R=RegisterMerchant(channel,username)
    R.Merchant_pc_register_moni()#虚拟微信账号
    # R.Merchant_pc_register_bang()#实体微信账号
    print("666")
